use eframe::egui::{self, Ui, FontId, RichText, Vec2, Align, Layout, Color32, Stroke, Rounding, Frame};
use crate::app::{App, state::AppEvent};
use crate::ui::theme::AppTheme;
use crate::ui::widgets::make_button;
use chrono::Local;

pub struct Dashboard;

impl Dashboard {
    pub fn render(ui: &mut Ui, app: &App, theme: &AppTheme) -> Option<AppEvent> {
        let mut event = None;
        
        ui.vertical_centered(|ui| {
            ui.add_space(32.0);
            
            // 显示时间
            let now = Local::now();
            let date_str = now.format("%Y-%m-%d").to_string();
            let time_str = now.format("%H:%M:%S").to_string();
            
            ui.label(
                egui::RichText::new(date_str)
                    .size(20.0)
                    .color(theme.text_secondary)
                    .strong()
            );
            
            ui.label(
                egui::RichText::new(time_str)
                    .size(38.0)
                    .color(theme.primary)
                    .strong()
            );
            
            ui.add_space(64.0);
            
            // 连接按钮
            let (btn_text, btn_color) = if app.connected {
                ("断开连接", theme.error)
            } else {
                ("连接网络", theme.success)
            };
            
            let btn_response = make_button(ui, btn_text, btn_color, [240.0, 60.0], 22.0, 12.0);
            if btn_response.clicked() {
                event = Some(AppEvent::ToggleConnection);
            }
            
            ui.add_space(16.0);
            
            // 当前服务器信息
            let server_text = if let Some(idx) = app.current_server_idx {
                if idx < app.servers.len() {
                    format!("当前服务器：{}", app.servers[idx].name)
                } else {
                    "未选择服务器".to_string()
                }
            } else {
                "未选择服务器".to_string()
            };
            
            ui.label(
                egui::RichText::new(server_text)
                    .size(16.0)
                    .color(theme.text_secondary)
            );
            
            ui.add_space(16.0);
            
            // 统计信息
            if app.connected {
                Dashboard::render_stats(ui, app, theme);
            }
        });
        
        event
    }
    
    fn render_stats(ui: &mut Ui, app: &App, theme: &AppTheme) {
        ui.group(|ui| {
            ui.set_width(ui.available_width() * 0.8);
            
            ui.vertical(|ui| {
                ui.horizontal(|ui| {
                    ui.label(egui::RichText::new("上传：").color(theme.text_secondary));
                    ui.label(
                        egui::RichText::new(format!("{:.2} MB", app.upload_usage))
                            .color(theme.primary)
                            .strong()
                    );
                    
                    ui.separator();
                    
                    ui.label(egui::RichText::new("下载：").color(theme.text_secondary));
                    ui.label(
                        egui::RichText::new(format!("{:.2} MB", app.download_usage))
                            .color(theme.primary)
                            .strong()
                    );
                });
                
                ui.add_space(8.0);
                
                ui.horizontal(|ui| {
                    ui.label(egui::RichText::new("已连接：").color(theme.text_secondary));
                    let duration = chrono::Duration::seconds(app.connection_time as i64);
                    let hours = duration.num_hours();
                    let minutes = duration.num_minutes() % 60;
                    let seconds = duration.num_seconds() % 60;
                    
                    ui.label(
                        egui::RichText::new(format!("{:02}:{:02}:{:02}", hours, minutes, seconds))
                            .color(theme.primary)
                            .strong()
                    );
                });
            });
        });
    }
} 